<?php
/**
 * Created by PhpStorm.
 * User: ouryun
 * Date: 2021/6/9
 * Time: 16:18
 */

namespace app\admin\controller\api;

use think\facade\Db;

/**
 * 处理微信模板通知
 * Class InformDispose
 * @package app\admin\controller\api
 */
class InformDispose
{

    /**
     * 处理巡店数据
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function patrol()
    {
        //查询未发送的数据
        $list = Db::name('lg_patrol_shop_warning')->alias('sw')
            ->join('lg_shop_clerks sc', 'sw.shop_clerks_id=sc.id', 'LEFT')
            ->where('sw.is_send_wxtpl', 1)
            ->field('sw.id,sw.shop_id,sw.shop_clerks_id,sc.name shop_clerks_name')
            ->limit(10)
            ->select()->toArray();
        if (empty($list)) {
            exit('没有可执行数据');
        }
        foreach ($list as $key => $value) {
            $arr = Db::name('lg_shop_admin')
                ->where('find_in_set (' . $value['shop_id'] . ', shop_id)')
                ->where('openid', '<>', '')
                ->field('id,name,openid,shop_id')
                ->select()->toArray();
            if (!empty($arr)) {
                foreach ($arr as $k => $vo) {
                    $result = WxTpl::sendPatrol($vo['openid'], $vo['name'], $value['shop_clerks_name']);
                    if (!empty($result)) {
                        $saveData = [
                            'openid' => $vo['openid'],
                            'wxtpl_id' => 'q_kJQ1aEHp0ZQFGxmNChUny5Dys-GpSfW-wJNhHlprY',
                            'shop_warning_id' => $value['id'],
                            'errcode' => $result['errcode'] ?? '',
                            'errmsg' => $result['errmsg'] ?? '',
                            'create_time' => time(),
                            'type' => 1
                        ];
                        //保存发送记录
                        Db::name('lg_wxtpl_send_record')->insert($saveData);
                        if (isset($result['errcode']) && $result['errcode'] == 0) {
                            $updateData = ['is_send_wxtpl' => 2, 'wxtpl_send_time' => time()];
                            Db::name('lg_patrol_shop_warning')->where('id', $value['id'])->update($updateData);
                        }
                    }
                }
            }
        }
        exit('end');
    }


    /**
     * 处理考勤数据
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function checkWorkAtt()
    {
        $list = Db::name('lg_shop_check_work_warning')->alias('ww')
            ->join('lg_distributor d', 'ww.shop_id=d.id', 'LEFT')
            ->where('ww.status', 2)->where('ww.wxtpl_send_status', 1)
            ->limit(10)
            ->field('ww.*,d.name shop_name')
            ->select()->toArray();
        if (empty($list)) {
            exit('没有可执行数据');
        }
        foreach ($list as $key => $value) {
            $arr = Db::name('lg_shop_admin')
                ->where('find_in_set (' . $value['shop_id'] . ', shop_id)')
                ->where('openid', '<>', '')
                ->field('id,name,openid,shop_id')
                ->select()->toArray();
            if (!empty($arr)) {
                foreach ($arr as $k => $vo) {
                    $result = WxTpl::sendCheck($vo['openid'], $value['shop_name']);
                    if (!empty($result)) {
                        $saveData = [
                            'openid' => $vo['openid'],
                            'wxtpl_id' => '7C5lRgFf_n2_sP82GSZjiKIt7H3K0s4qY5wKa_GMWWI',
                            'shop_warning_id' => $value['id'],
                            'errcode' => $result['errcode'] ?? '',
                            'errmsg' => $result['errmsg'] ?? '',
                            'create_time' => time(),
                            'type' => 2
                        ];
                        //保存发送记录
                        Db::name('lg_wxtpl_send_record')->insert($saveData);
                        if (isset($result['errcode']) && $result['errcode'] == 0) {
                            $updateData = ['is_send_wxtpl' => 2, 'wxtpl_send_time' => time()];
                            Db::name('lg_shop_check_work_warning')->where('id', $value['id'])->update($updateData);
                        }
                    }
                }
            }
        }
        exit('end');
    }
}
